home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 742 / rkrm_lib3 / rkrm_lib3.lha / Commodities / divert.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  8KB  |  212 lines

  1. ;/* divert.c - commodity to monitor user inactivity - compiled with SASC 5.10
  2. LC -b0 -cfist -v -j73 divert.c
  3. Blink FROM LIB:c.o,divert.o TO divert LIBRARY LIB:LC.lib,LIB:Amiga.lib NODEBUG SC SD
  4. quit; */
  5.  
  6. /*
  7. Copyright (c) 1992 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga, Inc. for
  10. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  11. published by Addison-Wesley (ISBN 0-201-56774-1).
  12.  
  13. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  14. information on the correct usage of the techniques and operating system
  15. functions presented in these examples.  The source and executable code
  16. of these examples may only be distributed in free electronic form, via
  17. bulletin board or as part of a fully non-commercial and freely
  18. redistributable diskette.  Both the source and executable code (including
  19. comments) must be included, without modification, in any copy.  This
  20. example may not be published in printed form or distributed with any
  21. commercial product.  However, the programming techniques and support
  22. routines set forth in these examples may be used in the development
  23. of original executable software products for Commodore Amiga computers.
  24.  
  25. All other rights reserved.
  26.  
  27. This example is provided "as-is" and is subject to change; no
  28. warranties are made.  All use is at your own risk. No liability or
  29. responsibility is assumed.
  30. */
  31.  
  32. #include <exec/libraries.h>
  33. #include <libraries/commodities.h>
  34. #include <dos/dos.h>
  35. #include <clib/exec_protos.h>
  36. #include <clib/alib_protos.h>
  37. #include <clib/alib_stdio_protos.h>
  38. #include <clib/commodities_protos.h>
  39. #include <devices/inputevent.h>
  40.  
  41. #ifdef LATTICE
  42. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  43. int chkabort(void) { return(0); }
  44. #endif
  45.  
  46. #define TIMER_CLICKS 100
  47.  
  48. void main(int, char **);
  49. void ProcessMsg(void);
  50. void CxFunction(CxMsg *, CxObj *);
  51.  
  52. struct Library *CxBase, *IconBase;
  53. struct MsgPort *broker_mp;
  54. CxObj *broker, *cocustom, *cosignal;
  55.  
  56. struct NewBroker newbroker =
  57. {
  58.     NB_VERSION,
  59.     "Divert",           /* string to identify this broker */
  60.     "Divert",
  61.     "show divert",
  62.     NBU_UNIQUE | NBU_NOTIFY,  /* Don't want any new commodities starting with this name. */
  63.     0, 0, 0, 0                /* If someone tries it, let me know                        */
  64. };
  65.  
  66. struct Task *task;
  67. ULONG cxsigflag, signal, cxobjsignal;
  68.  
  69. void main(int argc, char **argv)
  70. {
  71.     UBYTE **ttypes;
  72.     CxMsg *msg;
  73.  
  74.     if (CxBase = OpenLibrary("commodities.library", 37L))
  75.     {
  76.         /* open the icon.library for support library functions, ArgArrayInit() and ArgArrayDone() */
  77.         if (IconBase = OpenLibrary("icon.library", 36L))
  78.         {
  79.             if (broker_mp = CreateMsgPort())
  80.             {
  81.                 newbroker.nb_Port = broker_mp;
  82.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  83.  
  84.                 /* ArgArrayInit() is a support library function (in the 2.0 version of amiga.lib) */
  85.                 /* that makes it easy to read arguments from either a CLI or from Workbench's     */
  86.                 /* ToolTypes. Because it uses icon.library, the library has to be open before     */
  87.                 /* before calling this function.  ArgArrayDone() cleans up after this function.   */
  88.                 ttypes = ArgArrayInit(argc, argv);
  89.  
  90.                 /* ArgInt() (in amiga.lib) searches through the array set up by ArgArrayInit()    */
  91.                 /* for a specific ToolType.  If it finds one, it returns the numeric value of the */
  92.                 /* number that followed the ToolType (i.e., CX_PRIORITY=7).  If it  doesn't find  */
  93.                 /* the ToolType, it returns the default value (the third argument)                */
  94.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  95.  
  96.                 if (broker = CxBroker(&newbroker, NULL))
  97.                 {
  98.                     /* CxCustom() takes two arguments, a pointer to the custom function           */
  99.                     /* and an ID. Commodities Exchange will assign that ID to any CxMsg           */
  100.                     /* passed to the custom  function.                                            */
  101.                     if (cocustom = CxCustom(CxFunction, 0L))
  102.                     {
  103.                         AttachCxObj(broker, cocustom);
  104.  
  105.                         /* Allocate a signal bit for the signal CxObj */
  106.                         if ( (signal = (ULONG)AllocSignal(-1L)) != -1)
  107.                         {
  108.                             /* set up the signal mask */
  109.                             cxobjsignal = 1L << signal;
  110.                             cxsigflag |= cxobjsignal;
  111.  
  112.                             /* CxSignal takes two arguments, a pointer to the task to signal      */
  113.                             /* (normally the commodity) and the number of the signal bit the      */
  114.                             /* commodity acquired to signal with.                                 */
  115.                             task = FindTask(NULL);
  116.                             if (cosignal = CxSignal(task, signal))
  117.                             {
  118.                                 AttachCxObj(cocustom, cosignal);
  119.                                 ActivateCxObj(broker, 1L);
  120.                                 ProcessMsg();
  121.                             }
  122.                             FreeSignal(signal);
  123.                         }
  124.                     }
  125.                     /* DeleteCxObjAll() is a commodities.library function that not only deletes   */
  126.                     /* the CxObject pointed to in its argument, but it deletes all of the         */
  127.                     /* CxObjects that are attached to it.                                         */
  128.                     DeleteCxObjAll(broker);
  129.  
  130.                     /* Empty the port of all CxMsgs */
  131.                     while(msg = (CxMsg *)GetMsg(broker_mp))
  132.                         ReplyMsg((struct Message *)msg);
  133.                 }
  134.                 DeletePort(broker_mp);
  135.             }
  136.             ArgArrayDone();   /* this amiga.lib function cleans up after ArgArrayInit()           */
  137.             CloseLibrary(IconBase);
  138.         }
  139.         CloseLibrary(CxBase);
  140.     }
  141. }
  142.  
  143. void ProcessMsg(void)
  144. {
  145.     extern struct MsgPort *broker_mp;
  146.     extern CxObj *broker;
  147.     extern ULONG cxsigflag;
  148.     CxMsg *msg;
  149.     ULONG sigrcvd, msgid;
  150.     LONG returnvalue = 1L;
  151.  
  152.     while (returnvalue)
  153.     {
  154.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  155.  
  156.         while(msg = (CxMsg *)GetMsg(broker_mp))
  157.         {
  158.             msgid = CxMsgID(msg);
  159.             ReplyMsg((struct Message *)msg);
  160.  
  161.             switch(msgid)
  162.             {
  163.                 case CXCMD_DISABLE:
  164.                     ActivateCxObj(broker, 0L);
  165.                     break;
  166.                 case CXCMD_ENABLE:
  167.                     ActivateCxObj(broker, 1L);
  168.                     break;
  169.                 case CXCMD_KILL:
  170.                     returnvalue = 0L;
  171.                     break;
  172.                 case CXCMD_UNIQUE:
  173.                     returnvalue = 0L;
  174.                     break;
  175.             }
  176.         }
  177.  
  178.         if (sigrcvd & SIGBREAKF_CTRL_C) returnvalue = 0L;
  179.  
  180.         /* Check to see if the signal CxObj signalled us. */
  181.         if (sigrcvd & cxobjsignal) printf("Got Signal\n");
  182.     }
  183. }
  184.  
  185. /* The custom function for the custom CxObject.  Any code for a custom CxObj must be short        */
  186. /* and sweet because it runs as part of the input.device task.                                    */
  187. void CxFunction(register CxMsg *cxm, CxObj *co)
  188. {
  189.     struct InputEvent *ie;
  190.     static ULONG time = 0L;
  191.  
  192.     /* Get the struct InputEvent associated with this CxMsg. Unlike the InputEvent                */
  193.     /* extracted from a CxSender's CxMsg, this is a *REAL* input event, be careful with it.       */
  194.     ie = (struct InputEvent *)CxMsgData(cxm);
  195.  
  196.     /* This custom function counts the number of timer events that go by while no other input     */
  197.     /* events occur.  If it counts more than a certain amount of timer events, it clears the      */
  198.     /* count and diverts the timer event CxMsg to the custom object's personal                    */
  199.     /* list.  If an event besides a timer event passes by, the timer event count is reset.        */
  200.     if (ie->ie_Class == IECLASS_TIMER)
  201.     {
  202.         time++;
  203.         if (time >= TIMER_CLICKS)
  204.         {
  205.             time = 0L;
  206.             DivertCxMsg(cxm, co, co);
  207.         }
  208.     }
  209.     else
  210.         time = 0L;
  211. }
  212.